import axios,{InternalAxiosRequestConfig,AxiosResponse} from 'axios'
import {message} from 'antd'
import {SERVER_ERROR,TOKEN_OVERDUE,URL_NOT_FOUND} from '@/utils/constant'
//设置基础路径
axios.defaults.baseURL="http://www.zhaijizhe.cn:3005"
//设置请求的拦截器
axios.interceptors.request.use((config:InternalAxiosRequestConfig)=>{
    //从本地存储中获取token信息
    const token=localStorage.getItem('token')
    if(token){
        config.headers.Authorization=token
    }
    return config
})
//设置响应拦截器
axios.interceptors.response.use((response:AxiosResponse)=>{
    return response
},(error)=>{
    switch(error.response.status){
        case 500:
            message.warning(SERVER_ERROR)
            break
        case 401:
            message.warning(TOKEN_OVERDUE)
            break;
        case 404:
            message.warning(URL_NOT_FOUND)
            break;
    }
    return Promise.reject(error)
})
//导出axios
export default axios

